import {
  selectDistributionList,
  selectFirstDistribution,
  distributionDetail,
  distributionOperate,
  ableOperate,
  selectMdsDistList,
  deleteDistribution,
} from '@/services/distribution';
import prompt from '@/components/Prompt';

export default {
  namespace: 'distribution',

  state: {
    distributionList: {}, // 区域配送列表
    firstDistributionList: [],
    distributionDetail: {},
    mdsDistList: {},
  },

  effects: {
    /**
     * 查询区域配送列表
     */
    *selectDistributionList({ payload, callback }, { call, put }) {
      const response = yield call(selectDistributionList, payload);
      const { code, data } = response;
      if (code !== 0) return;
      const { list, pageSize, total, pageNum } = data;
      yield put({
        type: 'saveDistributionList',
        payload: {
          distributionList: {
            pagination: {
              current: pageNum,
              pageSize,
              total,
            },
            list,
          },
        },
      });
      callback && callback(list);
    },
    *selectFirstDistribution({ payload, callback }, { call, put }) {
      const response = yield call(selectFirstDistribution, payload);
      const { code, data } = response;
      if (code !== 0) return;
      // const { list, pageSize, total, pageNum } = data;
      yield put({
        type: 'saveFirstDistributionList',
        payload: {
          firstDistributionList: data,
        },
      });
      callback && callback(data);
    },
    //区域列表（层级结构）
    *selectMdsDistList({ payload, callback }, { call, put }) {
      const response = yield call(selectMdsDistList, payload);
      const { code, data } = response;
      if (code !== 0) return;
      // const { list, pageSize, total, pageNum } = data;
      yield put({
        type: 'MdsDistList',
        payload: {
          mdsDistList: data,
        },
      });
      callback && callback(data);
    },
    *distributionDetail({ payload, callback }, { call, put }) {
      const response = yield call(distributionDetail, payload);
      if (response.code === 0) {
        yield put({
          type: 'detail',
          payload: {
            distributionDetail: { [payload.id]: response.data },
          },
        });
        callback && callback(response.data);
      }
    },
    *distributionOperate({ payload, callback }, { call, put }) {
      const response = yield call(distributionOperate, payload);
      const { code, data, message } = response;
      const content = payload.id ? `编辑${message}` : `新增${message}`;
      if (code !== 0) return;
      prompt({ content });
      if (callback) callback(data);
    },
    *ableOperate({ payload, callback }, { call, put }) {
      const response = yield call(ableOperate, payload);
      const { code, data, message } = response;
      if (code !== 0) return;
      prompt({ content: message });
      if (callback) callback(data);
    },
    *deleteDistribution({ payload, callback }, { call, put }) {
      const response = yield call(deleteDistribution, payload);
      const { code, data, message } = response;

      if (code !== 0) return;
      prompt({ content: message });
      if (callback) callback(data);
    },
  },

  reducers: {
    // 区域配送列表
    saveDistributionList(state, { payload }) {
      return {
        ...state,
        distributionList: payload.distributionList,
      };
    },
    saveFirstDistributionList(state, { payload }) {
      return {
        ...state,
        firstDistributionList: [payload.firstDistributionList],
      };
    },
    MdsDistList(state, { payload }) {
      return { ...state, mdsDistList: payload.mdsDistList };
    },
    detail(state, { payload }) {
      return {
        ...state,
        distributionDetail: { ...state.distributionDetail, ...payload.distributionDetail },
      };
    },
  },
};
